<?php

namespace App\Remote\HuaweiPush;


use Closure;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;

class Remote extends \App\Remote\Remote
{
    static function getBaseUri(): string
    {
        // TODO: Implement getBaseUri() method.
        return "https://push-api.cloud.huawei.com";
    }

    public static function buildBasicHeaders(): array
    {
        $token = HuaweiPushAuthRemote::getToken();
        return [
            "Authorization" => vsprintf("%s %s", [
                attributeGet($token, "token_type", ""),
                attributeGet($token, "access_token", "")
            ]),
            "content-type"  => "application/json"
        ]; // TODO: Change the autogenerated stub
    }

    public static function registerResponseHandle(): Closure
    {
        return function ($responseHttpCode, $responseOriginalContents) {
            if ($responseHttpCode == ResponseAlias::HTTP_OK && isJson($responseOriginalContents)) {
                $responseArr = jsonToArr($responseOriginalContents);
                if ($responseArr["code"] == "80000000") {
                    return $responseArr;
                }
                throw new \Exception(vsprintf(
                    "requestId:%s | info:%s", [
                        attributeGet($responseArr, "requestId", "-"),
                        attributeGet($responseArr, "msg", "-")
                    ]
                ));
            }
            throw new \Exception($responseOriginalContents);
        }; // TODO: Change the autogenerated stub
    }
}
